package com.ffyc.ssm.controller;

import com.ffyc.ssm.common.CommonResult;
import com.ffyc.ssm.modle.News;
import com.ffyc.ssm.service.NewsService;
import com.ffyc.ssm.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@RestController
@RequestMapping(path = "/news")
public class NewsController {

    @Autowired
    private NewsService service;

    @GetMapping(path = "/list")
    public CommonResult showNewsList(){
        CommonResult<List> commonResult = null;
        try {
            List<News> list = service.findList();
            if (list!=null){
                commonResult = new CommonResult<>(200,"查询成功" , list);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }

    @GetMapping(path = "/passList")
    public CommonResult passList(){
        CommonResult<List> commonResult = null;
        try {
            List<News> list = service.passList();
            if (list!=null){
                commonResult = new CommonResult<>(200,"查询成功" , list);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }

    @GetMapping(path = "/findNews")
    public CommonResult findNews(@RequestParam("id") Integer id){//
        CommonResult<List> commonResult = null;
        try {
            News news = service.findNews(id);
            System.out.println(news);
            commonResult = new CommonResult(200,"查询成功" , news);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }

    //上传新闻信息(封面/标题..)
    @PostMapping(path = "/save")
    public  CommonResult addNews(@RequestParam("coverImg") CommonsMultipartFile file,
                                 @RequestParam("title") String title,
                                 @RequestParam("abstracts") String abstracts,
                                 @RequestParam("newsTypeId") String newsTypeId,
                                 @RequestParam("state") String state,
                                 @RequestParam("content") String content){
        CommonResult<String> commonResult =null;
        File f0 =new File("D:\\userImg\\coverImg");
        if (!f0.exists()){
            f0.mkdir();
        }
        String imgName = file.getOriginalFilename();
        String newFileName = StringUtil.newFileName(imgName);
        File f =new File(f0,newFileName);
        try {
            file.transferTo(f);
            Map<String,String> map = new HashMap<>();
            map.put("title",title);
            map.put("abstracts",abstracts);
            map.put("newsTypeId",newsTypeId);
            map.put("coverImg", newFileName);
            map.put("state", state);
            map.put("content", content);
            service.addNews(map);
            commonResult=new CommonResult(200,"上传成功",imgName);
        } catch (IOException e) {
            e.printStackTrace();
            commonResult =new CommonResult(500,"上传失败",null);
        }
        return  commonResult;
    }


    @PostMapping(path = "/update")
    public  CommonResult update(@RequestParam("coverImg") CommonsMultipartFile file,
                                 @RequestParam("title") String title,
                                 @RequestParam("abstracts") String abstracts,
                                 @RequestParam("newsTypeId") String newsTypeId,
                                 @RequestParam("content") String content,
                                 @RequestParam(value = "id",required = false) String id){
        CommonResult<String> commonResult =null;
        File f0 =new File("D:\\userImg\\coverImg");
        if (!f0.exists()){
            f0.mkdir();
        }
        String imgName = file.getOriginalFilename();
        String newFileName = StringUtil.newFileName(imgName);
        File f =new File(f0,newFileName);
        try {
            file.transferTo(f);
            Map<String,String> map = new HashMap<>();
            map.put("title",title);
            map.put("abstracts",abstracts);
            map.put("newsTypeId",newsTypeId);
            map.put("coverImg", newFileName);
            map.put("content", content);
            map.put("id", id);
            service.update(map);
            commonResult=new CommonResult(200,"上传成功",imgName);
        } catch (IOException e) {
            e.printStackTrace();
            commonResult =new CommonResult(500,"上传失败",null);
        }
        return  commonResult;
    }


    @PostMapping(path = "/delete")
    public CommonResult delete(Integer id){
        CommonResult commonResult = null;
        try {
            service.delete(id);
            commonResult = new CommonResult(200,"查询成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }

    @PostMapping(path = "/pass")
    public CommonResult pass(Integer id){
        CommonResult commonResult = null;
        try {
            service.pass(id);
            commonResult = new CommonResult(200,"查询成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }

    @PostMapping(path = "/unPass")
    public CommonResult unPass(Integer id){
        CommonResult commonResult = null;
        try {
            service.unPass(id);
            commonResult = new CommonResult(200,"查询成功" , null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult<>(500,"查询失败" , null);
        }
        return commonResult;
    }
}